<template>
  <div class="control">
    <el-card class="box-card">
      <h1>定时任务控制面板</h1>
      <div class="task-control">
        <el-form>
          <el-form-item
            v-for="(item) in schedule"
            :key="item.schedule_name"
            :label="convertName(item.schedule_name)"
            class="task-item"
            label-width="150px"
          >
            <el-input-number
              v-model="item.gap"
              @change="updateSchedule(item)"
              :min="1"
            ></el-input-number>
            <el-tag style="margin-left: 20px" type="danger">上次执行: {{ item.lastExecuteTime }}</el-tag>
            <el-tag style="margin-left: 20px" type="success">下次执行: {{ item.nextExecuteTime }}</el-tag>
          </el-form-item>
          <el-form-item style="text-align: center;">
            <el-button type="success" @click="getSchedules">刷新</el-button>
            <el-button type="primary" @click="saveSettings">保存修改</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Control",
  data() {
    return {
      schedule: [
      ],
    };
  },
  methods: {
    convertName(name) {
      if (name === "data_convert_hour") {
        return "数据预测频率(小时)";
      }
      if (name === "data_convert_day") {
        return "数据转储频率(天)";
      }
      if (name === "model_update") {
        return "模型更新(天)";
      }
      if (name === "data_clean") {
        return "数据清理";
      }
      return name; // 默认返回
    },
    // 计算下次执行日期（按天）
    calculateNextDate(lastExecuteTime, interval) {
      const timestamp = Date.parse(lastExecuteTime);
      const nextExecution = new Date(timestamp + interval * 24 * 60 * 60 * 1000);
      return nextExecution.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
      });
    },
    // 计算下次执行时间（按小时）
    calculateNextTime(lastExecuteTime, interval) {
      const timestamp = Date.parse(lastExecuteTime);
      const nextExecution = new Date(timestamp + interval * 60 * 60 * 1000);
      return nextExecution.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
      });
    },
    updateSchedule(item) {
      if (item.level === "hour") {
        item.nextExecuteTime = this.calculateNextTime(item.lastExecuteTime, item.gap);
      } else {
        item.nextExecuteTime = this.calculateNextDate(item.lastExecuteTime, item.gap);
      }
      return item;
    },
    // 保存用户设置
    saveSettings() {
      this.$axios.post(this.$httpUrlSchedule + '/update', this.schedule).then(res => {
        res = res.data
        console.log(res)
        if (res.code === 200) {
          // 更新成功
          this.$message.success('设置已保存!');
          this.getSchedules()
        } else {
          // 更新失败
          this.$message.error('设置保存失败!');
        }
      })
    },
    getSchedules() {
      this.$axios.get(this.$httpUrlSchedule + '/getAllSchedule').then(res => {
        res = res.data
        console.log(res)
        if (res.code === 200) {
          // 使用 map 遍历数据并更新每一项
          this.schedule = res.data.map(item => this.updateSchedule(item))
          this.$message.success('刷新定时任务成功!');
        }
      })
    }
  },
  mounted() {
    this.getSchedules()
  }
}
</script>
<style scoped>
.control {
  padding: 10px;
}
.task-control {
  margin-bottom: 15px;
}
.task-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
</style>
